﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WiXFileTool
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private string CreateFeatureRefs(string p)
        {
            string code = "<!-- directory: " + p + " -->" + Environment.NewLine;
            code += CreateFeatureRefs(System.IO.Directory.GetFiles(p));
            foreach (string dir in System.IO.Directory.GetDirectories(p))
            {
                code += CreateFeatureRefs(dir);
            }
            return code;
        }
        private string CreateFeatureRefs(string[] p)
        {
            string code = "";
            foreach (string file in p)
            {
                code += "<!-- sample component ref for " + file + " -->" + Environment.NewLine;
                code += "<ComponentRef Id=\"CMP" + file.GetHashCode().ToString().Replace('-', 'N') + "\" />" + Environment.NewLine;
            }
            return code;
        }

        private string CreateDirs(string p)
        {
            string code = "<Directory Id=\"DIR" + p.GetHashCode().ToString().Replace('-', 'N') + "\" Name=\"" + System.IO.Path.GetFileName(p) + "\">" + Environment.NewLine;
            foreach (string dir in System.IO.Directory.GetDirectories(p))
            {
                code += CreateDirs(dir);
            }
            code += "</Directory>" + Environment.NewLine;
            return code;
        }

        private string CreateCode(string p)
        {
            string code = "<!-- directory: " + p + " -->" + Environment.NewLine;
            code += "<DirectoryRef Id=\"DIR" + p.GetHashCode().ToString().Replace('-', 'N') + "\">" + Environment.NewLine;
            code += CreateCode(System.IO.Directory.GetFiles(p));
            code += "</DirectoryRef>" + Environment.NewLine;
            foreach (string dir in System.IO.Directory.GetDirectories(p))
            {
                code += CreateCode(dir);
            }
            return code;
        }

        private string CreateCode(string[] p)
        {
            string code = "";
            foreach (string file in p)
            {
                code += "<Component Id=\"CMP" + file.GetHashCode().ToString().Replace('-', 'N') + "\" Guid=\"" + Guid.NewGuid().ToString("B").ToUpper() + "\">" + Environment.NewLine;
                code += "<File Id=\"FILE" + file.GetHashCode().ToString().Replace('-', 'N') + "\" Source=\"" + file + "\" KeyPath=\"yes\"";
                if (System.IO.Path.GetExtension(file).ToLower().Equals(".exe"))
                {
                    code += " Checksum=\"yes\"";
                }
                code += "/>" + Environment.NewLine;
                code += "</Component>" + Environment.NewLine;
            }
            return code;
        }

        private void ctlDir_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog bd = new System.Windows.Forms.FolderBrowserDialog();
            if (bd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.ctlSource.Text = CreateDirs(bd.SelectedPath) + CreateCode(bd.SelectedPath) + CreateFeatureRefs(bd.SelectedPath);
            }
        }

        private void ctlFiles_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Multiselect = true;
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.ctlSource.Text = CreateCode(ofd.FileNames) + CreateFeatureRefs(ofd.FileNames);
            }
        }
    }
}
